export function os () {
    const u = navigator.userAgent
    const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1
    const isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
    return {
        isAndroid,
        isiOS
    }
}

export function setScroll (e, ref) {
    if (e) {
        let scrollName = (e.$route && e.$route.name) || e
        ref = ref.$el || ref
        let pageScroll = JSON.parse(sessionStorage.getItem('pageScroll') || '{}')
        let scroll = {
            left: ref.scrollLeft,
            top: ref.scrollTop
        }
        pageScroll[scrollName] = scroll
        sessionStorage.setItem('pageScroll', JSON.stringify(pageScroll))
    }
}

export function getScroll (e, ref) {
    if (e) {
        let scrollName = (e.$route && e.$route.name) || e
        ref = ref.$el || ref
        let pageScroll = JSON.parse(sessionStorage.getItem('pageScroll') || '{}')
        let scroll = pageScroll[scrollName]
        if (scroll) {
            ref.scrollTop = scroll.top
            ref.scrollLeft = scroll.left
            pageScroll[scrollName] = {}
            sessionStorage.setItem('pageScroll', JSON.stringify(pageScroll))
        }
    }
}

export function throttle (func, wait, options) {
    var context, args, result
    var timeout = null
    var previous = 0
    if (!options) options = {}
    var later = function () {
        previous = options.leading === false ? 0 : new Date().getTime()
        timeout = null
        result = func.apply(context, args)
        if (!timeout) context = args = null
    }
    return function () {
        var now = new Date().getTime()
        if (!previous && options.leading === false) previous = now
        var remaining = wait - (now - previous)
        context = this
        args = arguments
        if (remaining <= 0 || remaining > wait) {
            if (timeout) {
                clearTimeout(timeout)
                timeout = null
            }
            previous = now
            result = func.apply(context, args)
            if (!timeout) context = args = null
        } else if (!timeout && options.trailing !== false) {
            timeout = setTimeout(later, remaining)
        }
        return result
    }
}

export function debounce (func, wait, immediate) {
    var timeout, args, context, timestamp, result

    var later = function () {
        var last = new Date().getTime() - timestamp

        if (last < wait && last >= 0) {
            timeout = setTimeout(later, wait - last)
        } else {
            timeout = null
            if (!immediate) {
                result = func.apply(context, args)
                if (!timeout) context = args = null
            }
        }
    }

    return function () {
        context = this
        args = arguments
        timestamp = new Date().getTime()
        var callNow = immediate && !timeout
        if (!timeout) timeout = setTimeout(later, wait)
        if (callNow) {
            result = func.apply(context, args)
            context = args = null
        }

        return result
    }
}

export function getImgUrl (image, type) {
    let imgType = type || 'source'
    let imgHost = process.env.VUE_APP_IMGURL
    let result = ''
    if (!image) return
    if (typeof image == 'object') {
        result = imgHost + image[imgType]
    }
    if (typeof image == 'string') {
        result = imgHost + image
    }
    return result
}

export function isWechat () {
    var uExplorer = window.navigator.userAgent.toLowerCase()
    if (uExplorer.match(/MicroMessenger/i) == 'micromessenger') { // 微信内核
        return true
    } else {
        return false
    }
}
